﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
using System.Xml.Linq;
using System.Runtime.InteropServices;
using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 图片
    /// </summary>
    public class Image : DrawingElement
    {
        #region API 函数
        /// <summary>
        /// 从文件创建图像
        /// </summary>
        /// <param name="image"></param>
        /// <param name="file_path">文件路径</param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUImageCreateFromFile(out IntPtr image, [MarshalAs(UnmanagedType.LPUTF8Str)] string file_path);

        /// <summary>
        /// 从图像数据创建图片
        /// </summary>
        /// <param name="image"></param>
        /// <param name="image_rep"><see cref="Objects.ImageRep"/>图像数据</param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUImageCreateFromImageRep(out IntPtr image, IntPtr image_rep);

        /// <summary>
        /// 获取图像数据
        /// </summary>
        /// <param name="image"></param>
        /// <param name="image_rep">返回 <see cref="Objects.ImageRep"/></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUImageGetImageRep(IntPtr image, out IntPtr image_rep);

        /// <summary>
        /// 获取图片名字
        /// </summary>
        /// <param name="image"></param>
        /// <param name="name"><see cref="StringRef"/></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUImageGetName(IntPtr image, ref IntPtr name);

        /// <summary>
        /// 设置图片名字
        /// </summary>
        /// <param name="image"></param>
        /// <param name="name">名字</param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUImageSetName(IntPtr image, [MarshalAs(UnmanagedType.LPUTF8Str)] string name);

        /// <summary>
        /// 获取图片变换
        /// </summary>
        /// <param name="image"></param>
        /// <param name="transform">变换</param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUImageGetTransform(IntPtr image, ref Transformation transform);

        /// <summary>
        /// 应用图片变换
        /// </summary>
        /// <param name="image"></param>
        /// <param name="transform"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUImageSetTransform(IntPtr image, Transformation transform);

        /// <summary>
        /// 获取图片文件名称
        /// </summary>
        /// <param name="image"></param>
        /// <param name="file_name"><see cref="StringRef"/></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUImageGetFileName(IntPtr image, ref IntPtr file_name);

        /// <summary>
        /// 获取图片尺寸
        /// </summary>
        /// <param name="image"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUImageGetDimensions(IntPtr image, out double width, out double height);

        /// <summary>
        /// 获取图片定义
        /// </summary>
        /// <param name="image"></param>
        /// <param name="component"><see cref="ComponentDefinition"/></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUImageGetDefinition(IntPtr image, ref IntPtr component);

        /// <summary>
        /// 获取像素个数
        /// </summary>
        /// <param name="image"></param>
        /// <param name="width">列数</param>
        /// <param name="height">行数</param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUImageGetPixelDimensions(IntPtr image, out int width, out int height);

        /// <summary>
        /// 获取像素数据
        /// </summary>
        /// <remarks>传入的数组必须足够大才能接收全部数据，可以使用 SUImageGetImageRep 获取ImageRep 后，再调用ImageRep的 SUImageRepGetDataSize方法获取需要的数组长度</remarks>
        /// <param name="image"></param>
        /// <param name="data_size"></param>
        /// <param name="pixel_data"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUImageGetData(IntPtr image, long data_size, byte[] pixel_data);
        #endregion

        /// <summary>
        /// 图像定义
        /// </summary>
        public ComponentDefinition Definition
        {
            get
            {
                ComponentDefinition c = new ComponentDefinition();
                SketchUpExceptionHelper.ThrowSUResult(SUImageGetDefinition(Id, ref c.Id));
                return c;
            }
        }

        /// <summary>
        /// 图像数据
        /// </summary>
        public ImageRep ImageRep
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUImageGetImageRep(Id, out IntPtr rep));
                return new ImageRep(rep);
            }
        }

        /// <summary>
        /// 图像名称
        /// </summary>
        public string Name
        {
            get
            {
                StringRef s = StringRef.Empty;
                SketchUpExceptionHelper.ThrowSUResult(SUImageGetName(Id, ref s.Id));
                return s.Dispose();
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUImageSetName(Id, value));
            }
        }

        /// <summary>
        /// 获取图片文件名
        /// </summary>
        public string FileName
        {
            get
            {
                StringRef s = StringRef.Empty;
                SketchUpExceptionHelper.ThrowSUResult(SUImageGetFileName(Id, ref s.Id));
                return s.Dispose();
            }
        }

        /// <summary>
        /// 图像变换
        /// </summary>
        public Transformation Transform
        {
            get
            {
                Transformation t = new Transformation();
                SketchUpExceptionHelper.ThrowSUResult(SUImageGetTransform(Id, ref t));
                return t;
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUImageSetTransform(Id, value));
            }
        }

        internal Image(IntPtr id) : base(id) { }

        /// <summary>
        /// 从文件创建图像
        /// </summary>
        /// <param name="file"></param>
        public Image(string file)
        {
            SketchUpExceptionHelper.ThrowSUResult(SUImageCreateFromFile(out Id, file));
        }

        /// <summary>
        /// 从图像数据创建
        /// </summary>
        /// <param name="rep"></param>
        public Image(ImageRep rep)
        {
            SketchUpExceptionHelper.ThrowSUResult(SUImageCreateFromImageRep(out Id, rep.Id));
        }

        /// <summary>
        /// 获取图片物件的尺寸
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public void GetSize(out double width, out double height)
        {
            SketchUpExceptionHelper.ThrowSUResult(SUImageGetDimensions(Id, out width, out height));
        }

        public void GetPixelSize(out int width, out int height)
        {
            SketchUpExceptionHelper.ThrowSUResult(SUImageGetPixelDimensions(Id, out width, out height));
        }
    }
}